'''
Created on 29/03/2014

@author: juampi
'''

class Chords(object):
    
    def __init__(self,offset,position):        
        self.position = position
        self.offset = offset
    
    def __str__(self):
        return 'Chord %s => (%s)' % (self.offset,self.position)
    
    def __repr__(self):
        return 'Chord %s => (%s)' % (self.offset,self.position)
    
    def __cmp__(self,other):
        return cmp(self.position,other.position) and cmp(self.offset,other.self.offset) 

class Separator(object):
        
    def __str__(self):
        return '|'

class Tab(object):
    '''
    classdocs
    '''

    def __init__(self,instrument,tabsize,chords):
        '''
        Constructor
        '''
        self.instrument = instrument
        self.chords = chords
        self.tabsize = tabsize
    
    
    def render(self):
        tabchords = []
        for istring in self.instrument.strings:                        
            tabchord = '-' * self.tabsize                                
            tabchords.append(tabchord)
        
        for chord in self.chords:            
            tabstring = list(tabchords[chord.position.string])
            fret = str(chord.position.fret)            
            for x in range(len(fret)):
                tabstring[chord.offset + x] = fret[x]
                tabchords[chord.position.string] = ''.join(tabstring)         
        
        for x in range(len(tabchords)):
            tabchords[x] = '%s%s%s%s' % (self.instrument.strings[x].get(0),Separator(),tabchords[x],Separator())
                               
        output = tabchords            
        output_string = '\n'.join(output)
        output_string = '\n' + output_string + '\n'
        return output_string 

        